package com.faceye.core;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories(repositoryBaseClass = com.faceye.core.repository.jpa.impl.BaseRepositoryImpl.class, basePackages = {
"com.faceye.*.repository.jpa" }, enableDefaultTransactions = true, repositoryImplementationPostfix = "Impl")
@EntityScan(basePackages={"com.faceye.*.entity"})
@EnableTransactionManagement
public class JpaConfiguration {
	@Bean(name="dataSource", destroyMethod="close")
	@ConfigurationProperties(prefix="spring.datasource")
	public DataSource dataSource() {
		DataSource dataSource = null;
		dataSource = DataSourceBuilder.create().build();
		return dataSource;
	}
	
	@Bean
	public JpaVendorAdapter jpaVendorAdapter() {
		HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
		jpaVendorAdapter.setGenerateDdl(true);
		jpaVendorAdapter.setShowSql(true);
		jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");
		return jpaVendorAdapter;
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
		entityManagerFactoryBean.setPackagesToScan(new String[]{"com.faceye.*.entity"});
		entityManagerFactoryBean.setDataSource(dataSource());
		entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
		Properties properties = new Properties();
		properties.setProperty("hibernate.show_sql", "true");
		properties.setProperty("hibernate.jdbc.fetch_size", "100");
		properties.setProperty("hibernate.hbm2ddl.auto", "update");
		properties.setProperty("hibernate.query.substitutions", "true 1, false 0");
		properties.setProperty("hibernate.default_batch_fetch_size", "30");
		properties.setProperty("hibernate.max_fetch_depth", "2");
		properties.setProperty("hibernate.generate_statistics", "true");
		properties.setProperty("hibernate.bytecode.use_reflection_optimizer", "true");
//		properties.setProperty("hibernate.cache.use_second_level_cache", "true");
//		properties.setProperty("hibernate.cache.use_query_cache", "true");
//		properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
		entityManagerFactoryBean.setJpaProperties(properties);
		//不可同时配置setPackagsToScan与 setPersistenceUnitName,否则会出现 'entity' Not a managed type 异常.
//		entityManagerFactoryBean.setPersistenceUnitName("feature");
		return entityManagerFactoryBean;
	}
}
